#coding=utf-8
#Boa:Dialog:DlgCustRegionEditor

import wx

from product import CDataCustomerRegion

def create(parent):
    return DlgCustRegionEditor(parent)

[wxID_DLGCUSTREGIONEDITOR, wxID_DLGCUSTREGIONEDITORBTNCANCEL, 
 wxID_DLGCUSTREGIONEDITORBTNCONFIRM, wxID_DLGCUSTREGIONEDITORSTATICTEXT1, 
 wxID_DLGCUSTREGIONEDITORTCCODE, wxID_DLGCUSTREGIONEDITORTCMEMO, 
 wxID_DLGCUSTREGIONEDITORTCNAME, wxID_DLGCUSTREGIONEDITORTCODE, 
 wxID_DLGCUSTREGIONEDITORTNAME, 
] = [wx.NewId() for _init_ctrls in range(9)]

class DlgCustRegionEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGCUSTREGIONEDITOR,
              name=u'DlgCustRegionEditor', parent=prnt, pos=wx.Point(487, 364),
              size=wx.Size(537, 233), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u7f16\u8f91\u5730\u533a')
        self.SetClientSize(wx.Size(529, 199))
        self.Center(wx.BOTH)
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))

        self.tName = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORTNAME,
              label=u'\u540d\u79f0\uff1a', name=u'tName', parent=self,
              pos=wx.Point(48, 40), size=wx.Size(51, 19), style=0)

        self.tcName = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(104, 32),
              size=wx.Size(184, 28), style=0, value=u'')
        self.tcName.Bind(wx.EVT_TEXT, self.OnTcNameText,
              id=wxID_DLGCUSTREGIONEDITORTCNAME)

        self.staticText1 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT1,
              label=u'\u8bf4\u660e\uff1a', name='staticText1', parent=self,
              pos=wx.Point(48, 96), size=wx.Size(51, 19), style=0)

        self.tcMemo = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCMEMO,
              name=u'tcMemo', parent=self, pos=wx.Point(104, 88),
              size=wx.Size(384, 28), style=0, value=u'')

        self.btnConfirm = wx.Button(id=wxID_DLGCUSTREGIONEDITORBTNCONFIRM,
              label=u'\u786e\u5b9a', name=u'btnConfirm', parent=self,
              pos=wx.Point(96, 160), size=wx.Size(86, 29), style=0)
        self.btnConfirm.Bind(wx.EVT_BUTTON, self.OnBtnConfirmButton,
              id=wxID_DLGCUSTREGIONEDITORBTNCONFIRM)

        self.btnCancel = wx.Button(id=wxID_DLGCUSTREGIONEDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(296, 160), size=wx.Size(86, 29), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGCUSTREGIONEDITORBTNCANCEL)

        self.tCode = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORTCODE,
              label=u'\u7b80\u7801\uff1a', name=u'tCode', parent=self,
              pos=wx.Point(328, 40), size=wx.Size(51, 19), style=0)

        self.tcCode = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCCODE,
              name=u'tcCode', parent=self, pos=wx.Point(384, 32),
              size=wx.Size(100, 28), style=0, value=u'')

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.m_region = None

    def setEditData(self, regionObj):
        self.m_region = regionObj
        self.tcName.SetValue(regionObj.getValue(regionObj.fi_name))
        self.tcCode.SetValue(regionObj.getValue(regionObj.fi_code))
        self.tcMemo.SetValue(regionObj.getValue(regionObj.fi_memo))
        
    def OnBtnConfirmButton(self, event):
        name = self.tcName.GetValue()
        if (name == ''):
            dlg = wx.MessageDialog(self, u'必须设置区域名称', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        if self.m_region:
            self.m_region.setValue(CDataCustomerRegion.fi_name, self.tcName.GetValue())
            self.m_region.setValue(CDataCustomerRegion.fi_code, self.tcCode.GetValue())
            self.m_region.setValue(CDataCustomerRegion.fi_memo, self.tcMemo.GetValue())
            self.m_region.update()
        else:
            CDataCustomerRegion.add(name, self.tcMemo.GetValue(), self.tcCode.GetValue())
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)

    def OnTcNameText(self, event):
        import config
        name = self.tcName.GetValue()
        code = config.CBriefCode.getBriefCode(name)
        self.tcCode.SetValue(code)
